;(function(FormBase) {
    FormBase.FormBtn = FormBase.extend({
        alreadySubmit: false, //是否已经提交,
        submitConfig: {},
        buttonText: '确定',
        submitText: '提交中...',
        constructor: function(formEleId, mainContent, formConfig) {
            this.submitConfig = formConfig.submitConfig ? formConfig.submitConfig : {};
            this.formEleId = formEleId;
            this.formEle = document.getElementById(formEleId);
            this.mainContent = mainContent;
            this.buttonText = mainContent.buttonText || this.buttonText;
            this.submitText = mainContent.submitText || this.submitText;
            var _this = this;
            this.mainContent.spins = function(searchs, text) {
              _this.spins(searchs, text)
            }
            this.mainContent.spinno = function(searchs, text) {
              _this.spinno(searchs, text)
            }
            this.initMouseListeners();
        },
        initMouseListeners: function() {
            var _this = this;
            if (document.all) { // IE
                this.formEle.attachEvent("onclick", this.clickAction.bindAsEventListener(this));
            } else {
                this.formEle.addEventListener("click", this.clickAction.bindAsEventListener(this), false);
            }
            //回车事件
            // $(this.formEle).keydown(function(e) {
            //     if (e.keyCode == 13) {
            //         _this.mouseEnter();
            //     }
            // });

            document.onkeydown = function(e) {
                var theEvent = window.event || e;
                var code = theEvent.keyCode || theEvent.which;
                if (code == 13) {
                    //触发所有表单 blur 事件
                    var allElement = $("[" + _this.mainContent.attrName + "='" + _this.mainContent.formId + "']").find("[" + _this.mainContent.attrName + "-config]");
                    for (var i = 0; i < allElement.length; i++) {
                        $(allElement[i]).blur();
                    }
                    setTimeout(function(){
                        $(_this.formEle).click();
                    },10);
                    
                }
            }
        },
        // mouseEnter: function() {
        //     //触发所有表单 blur 事件
        //     var allElement = $("[" + this.mainContent.attrName + "='" + this.mainContent.formId + "']").find("[" + this.mainContent.attrName + "-config]");
        //     for (var i = 0; i < allElement.length; i++) {
        //         $(allElement[i]).blur();
        //     }
        //     this.clickAction();
        // },
        clickAction: function() {
            //重新校验
            var formObjs = this.mainContent.formObject[this.mainContent.formId];
            var elementArr = this.mainContent.elementArr[this.mainContent.formId];
            for(var i =0;i<elementArr.length; i++){
                if(formObjs[elementArr[i]].formType == "Checkbox") ;
                else  formObjs[elementArr[i]].blurAction();
            }
            var result = this.checkStatus();
            var _this = this;

            if (!result) {
                //清空上次错误信息
                this.mainContent.clearErrors();
                this.mainContent.showErrors();
                return;
            } else if (this.alreadySubmit) {
                //防止重复提交
                return;
            } else {
                this.mainContent.clearErrors();
                //是否有预处理
                if (this.mainContent.submitBefore) {
                    this.mainContent.submitBefore(function() {
                        _this.spins()
                        //提交至后台处理
                        if (_this.submitConfig.nextPath) {
                            _this.mainContent.submitSuccess(this.submitConfig.nextPath);
                        } else {
                            var sendData = _this.getSendData();
                            _this.doPost(sendData.url, sendData.data, {
                                success: function(data) {
                                    if(data.head.returnCode === 'OK') {
                                        _this.mainContent.submitSuccess(data);
                                    } else {
                                        _this.mainContent.submitFail(data);
                                        _this.alreadySubmit = false;
                                        _this.spinno()
                                    }
                                    
                                },
                                error: function(data) {
                                    _this.mainContent.submitFail(data);
                                    _this.alreadySubmit = false;
                                    _this.spinno()
                                }
                            });
                        }
                    });
                } else {
                    //提交至后台处理
                    if (this.submitConfig.nextPath) {
                        this.mainContent.submitSuccess(this.submitConfig.nextPath);
                    } else {
                        var sendData = this.getSendData();
                        _this.spins()
                        this.doPost(sendData.url, sendData.data, {
                            success: function(data) {
                                if(data.head.returnCode === 'OK') {
                                    _this.mainContent.submitSuccess(data);
                                } else {
                                    _this.mainContent.submitFail(data);
                                    _this.spinno()
                                    _this.alreadySubmit = false;
                                }
                              
                            },
                            error: function(data) {
                                _this.mainContent.submitFail({
                                    head: {
                                        returnCode: '02',
                                        returnMsg: '网络异常，请刷新重试'
                                    }
                                });
                                _this.spinno()
                                _this.alreadySubmit = false;
                            }
                        });
                    }
                }

            }
        },
        spins: function (searchs, text) {
          this.formEleId = this.formEleId || searchs
          var _text = typeof text === 'undefined' ? this.submitText : (text || '')
          $('#' + this.formEleId).addClass('searchs').html('<i id="searchs" class="pr h20 in_b mr5"></i>' + _text);
        },
        spinno: function (searchs, txt) {
          this.formEleId = this.formEleId || searchs
          $('#' + this.formEleId).removeClass('searchs').html(txt || this.buttonText);
        },
        getSendData: function() {
            var data, url,
                submitConfig = this.submitConfig;
            url = submitConfig.url;
            data = this.getCurrentValue(submitConfig.data);
            return {
                url: url,
                data: data
            };
        },
        checkStatus: function() {
            for (var key in this.mainContent[this.mainContent.formId + "errForm"]) {
                return false;
            }
            return true;
        }
    });
})(window.FormBase = window.FormBase || {})
